<?php
/**
 *ProductPage Model
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * ProductPage_Model
 * Provides method to display the correct content of product pages
 *@package model
 */
class ProductPage_Model extends Model{
        /**
     * Reference to database connection
     * @var object
     */
    private $oDBInstance;

    /**
     * Model constructor
     * Establishes connection to database
     */
    public function __construct(){
                /**
         * Gets the database reference from Mysql_dbconnection
         * @var object
         */
        $this->oDBInstance = Mysql_dbconnection::getInstance();
    }
/**
 * Gets the page title which belongs to the product id
 * @param integer product ID
 * @param integer language
 */
public function getPageTitle($iPage, $iLanguage){
        //Prevents from SQL-Injections
        $iLanguage = $this->oDBInstance->getSQLValueString($iLanguage,'int');
         $iPage = $this->oDBInstance->getSQLValueString($iPage,'int');
        
        //Sets table for query
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND lid = $iLanguage) as a");
        
        //Sets column for WHERE clause in query
        $this->oDBInstance->_setDefaultColumn('id');
        
        //Performs query on database
        $this->oDBInstance->performSelect('name', '', '', '', "$iPage");
        //Fetches result of query
        $aResult = $this->oDBInstance->fetch('array');
        
        if(isset($aResult[0]['name'])){
            return $aResult[0]['name'];
            
        }else{
            return "Kein Seitentitel gefunden";
        }
        
    }
/**
 * Gets an array of the different product categories
 * @param integer page ID
 * @param integer language
 */
 public function getCategories($iPid, $iLanguage){
        //Sets table (join) for query
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name, position, visible, parent, level
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND cms_only = 0 AND type = 'category' AND lid = $iLanguage) as a");
        //Sets column for WHERE clause in query
        $this->oDBInstance->_setDefaultColumn('id');
        $sParentID = $this->oDBInstance->getSQLValueString($iPid,'string');
        if ($iPid != ''){
            $this->oDBInstance->performSelect('id, name, position, visible, parent, level', '', "a.parent = $sParentID ORDER BY position");
            //Fetches the query result in an array
            $aCategories = $this->oDBInstance->fetch('array');
            if(isset($aCategories[0])){
                return $aCategories;
            }else{
                return "Es wurden noch keine Kategorien angelegt.";
            }
        }else{
            return "Es wurde keine Produktseite angegeben.";
        }
    }
/**
 * Connects to the database and loads all products which belongs to the passed category 
 * @param integer category ID
 * @param integer language
 */
public function getProducts($iCategory, $iLang){  
            //Performs a query on the database
            $this->oDBInstance->query("SELECT *
                        FROM `bs_product` AS p
                        INNER JOIN `bs_product_language` AS l ON p.id = l.pid
                        INNER JOIN bs_language as t ON l.lid = t.id INNER JOIN `bs_image` AS i ON p.mainimage = i.id
                        WHERE p.category = '$iCategory' AND l.lid = '$iLang' ORDER BY p.position DESC;");
            //Fetches the query result in an array
            $aProducts = $this->oDBInstance->fetch('array');
            if(isset($aProducts[0])){
                return $aProducts;
        }else{
            return "In dieser Kategorie sind noch keine Produkte vorhanden.";
        }
    }
/**
 * Loads the product details of a product ID
 * @param integer product ID
 * @param integer language
 */
    public function getProductDetail($iPid, $iLang){
        //Performs a query on the database
        $this->oDBInstance->query("SELECT p.id, p.name, p.mainimage, l.short_description, l.long_description, i.link
                        FROM `bs_product` AS p
                        INNER JOIN `bs_product_language` AS l ON p.id = l.pid
                        INNER JOIN bs_image AS i ON p.mainimage = i.id
                        WHERE p.id = '$iPid' AND l.lid = '$iLang'
                        LIMIT 0 , 30");
        //Fetches the query result in an array
        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0])){
            return $aResult[0];
        }else{
            echo "Fehler bei DB-Abfrage von Produktdetails, leeres Resultat!";
            exit();
        }
         

    }
/**
 * DEPRICATED gets product attributes
 * @param integer product ID
 * @param string attributes
 * @param integer language
 */
    public function getProductAttribut($iProductID, $sAttribut, $iLang=''){
        if ($iLang !=''){
            $this->oDBInstance->query("SELECT $sAttribut
                        FROM `bs_product` AS p
                        INNER JOIN `bs_product_language` AS l ON p.id = l.pid
                        WHERE p.id = '$iProductID' AND l.lid = '$iLang'
                        LIMIT 0 , 30");
            $aResult = $this->oDBInstance->fetch('array');
            return $aResult[0][$sAttribut];
        }else{
            $this->oDBInstance->query("SELECT $sAttribut
                        FROM `bs_product` 
                        WHERE id = '$iProductID'
                        LIMIT 0 , 30");

            $aResult = $this->oDBInstance->fetch('array');
            return $aResult[0][$sAttribut];
        }
    }
/**
 * Gets an array of product images which belongs to the product ID
 * @param integer product ID
 */
    public function getProductImages($iPid){
        //Performs a query on the database
        $this->oDBInstance->query("SELECT i.id, i.link
                        FROM `bs_product` AS p
                        INNER JOIN bs_image AS i ON  p.id = i.product
                        WHERE p.id = '$iPid'
                        LIMIT 0 , 30");
        //Fetches the query result in an array
        return $this->oDBInstance->fetch('array');

    }
/**
 * Updates product details 
 * @param integer product ID
 * @param string product name
 * @param string short description
 * @param string long description
 * @param integer main image ID
 * @param integer language
 */
    public function updateProduct($iProductID, $sName, $sShort_description, $sLong_description, $iMainimage,$iLang){
        $iProductID = $this->oDBInstance->getSQLValueString($iProductID, 'int');
        $sName = $this->oDBInstance->getSQLValueString($sName, 'string');
        $sShort_description = $this->oDBInstance->getSQLValueString($sShort_description, 'string');
        $sLong_description = $this->oDBInstance->getSQLValueString($sLong_description, 'string');
        $iMainimage = $this->oDBInstance->getSQLValueString($iMainimage, 'int');
        $iLang = $this->oDBInstance->getSQLValueString($iLang, 'int');

        $this->oDBInstance->query("UPDATE bs_product SET name = $sName, mainimage = $iMainimage WHERE id = $iProductID");
        $this->oDBInstance->query("UPDATE bs_product_language SET short_description = $sShort_description, long_description = $sLong_description WHERE pid = $iProductID AND lid = $iLang");
    }
/**
 * Deletes a product from database
 * @param integer product ID
 */
    public function deleteProduct($iProductID){
        $iProductID = $this->oDBInstance->getSQLValueString($iProductID, 'int');

        $this->oDBInstance->query("DELETE FROM bs_image WHERE product = '$iProductID'");
        $this->oDBInstance->query("DELETE FROM bs_product_language WHERE pid = '$iProductID'");
        $this->oDBInstance->query("DELETE FROM bs_product WHERE id = '$iProductID'");
    }
    
/**
 * Updates the product structure 
 * @param array product order
 */
    public function updateProductStructure($aProductOrder){
        //Count all elements in the array 
        $iPosCounter = count($aProductOrder);
        foreach ($aProductOrder as $sProductID){
        if($sProductID!= ""){
        $this->oDBInstance->query("UPDATE bs_product SET position='$iPosCounter' WHERE id='$sProductID'");
        $iPosCounter--;
        }
        }
    }
/**
 * Updates the category structure 
 * @param array category order
 */
public function updateCategoryStructure($aCategoryOrder){
        $iPosCounter = 0; //count($aCategoryOrder);
        foreach ($aCategoryOrder as $sCategoryID){
        if($sCategoryID!= ""){
        $this->oDBInstance->query("UPDATE bs_page SET position='$iPosCounter' WHERE id='$sCategoryID'");
        $iPosCounter++;//--;
        }
        }
    }
/**
 * Checks if page ID is a category page or an content page 
 * @param integer page ID
 */
     public function isCategory($iPage){
        $iProductID = $this->oDBInstance->getSQLValueString($iPage, 'int');
        $this->oDBInstance->query("SELECT type FROM bs_page WHERE id = '$iPage'");
        
        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0]['type'])){
            if($aResult[0]['type'] == 'category'){
                return TRUE;
            }
        }
        return FALSE;
    }
/**
 * Gets parent page of categories 
 * @param integer category ID
 */
    public function getParentProductPage($iCategory){
        $iCategory = $this->oDBInstance->getSQLValueString($iCategory, 'int');
        
        $this->oDBInstance->query("SELECT parent FROM bs_page WHERE id = $iCategory");
        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0])){
            return $aResult[0]['id'];
        }else{
            return FALSE;
        }
    }
    


}
?>